package com.liang.seven_Topic_Exchanges;

import com.rabbitmq.client.*;
import utils.RabbitmqUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/18
 * Time: 20:24
 * Description: 声明主题 交换机 相关队列
 *
 * 消费者 C2
 *
 */
public class ReceiveLogsTopic02 {


    //交换机的名称
    public static final String EXCHANGES_NAME = "topic_logs";

    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbitmqUtil.getChannel();

        //声明交换机
        channel.exchangeDeclare(EXCHANGES_NAME, BuiltinExchangeType.TOPIC);


        //声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName,true,false,false,null);

        //交换机 绑定队列
        channel.queueBind(queueName,EXCHANGES_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGES_NAME,"lazy.#");



        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                System.out.println(new String(message.getBody(),"UTF-8"));
                System.out.println("接受队列:"+queueName+"   绑定键"+message.getEnvelope().getRoutingKey());
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("消费中断:"+consumerTag);
            }
        };

        channel.basicConsume(queueName,true,deliverCallback, cancelCallback);



    }


}